# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::layer::Nation;
use canvas::Color;
use Settings;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{Nations} = [];
	open FILE, "<:utf8", $Settings::DataPath."globe.csv";
	my $line = <FILE>;
	while (my $line = <FILE>)
	{
		my ($layer, $name, $notes, $text, $x, $y, $coords, $line_color, $fill_color, $text_color, $text_size, $text_type, $line_size, $line_type, $fill_mode, $type, $visible, $radius) = split /;/, $line;
		push @{$self->{Nations}}, [$coords, $fill_color, $text_color, $x, $y, $text, $text_size];
	}	
	close FILE;
	bless ($self, $class);
	return $self;
}

sub drawTerritory
{	
	my $self = shift;
	my $poly = shift;
	my @color = @_;
	my $canvas = $self->{Canvas};
	my $fg = dcExplorer::canvas::Color->new(@color, 0);
	my $bg = dcExplorer::canvas::Color->new(@color, 96);
	my $points = [];
	push @{$points}, [$self->{Canvas}->center(split /:/, $_)] foreach (split /\s*-\s*/, $poly);
	$canvas->Poly($points, $fg, $bg);
}

sub display
{
	my $self = shift;
	$self->{Canvas} = shift;
	for my $nation (@{$self->{Nations}})
	{
		my ($coords, $fill_color, $text_color, $x, $y, $text, $text_size) = @{$nation};
		$self->drawTerritory($coords, int($fill_color / 256 / 256) % 256, int($fill_color / 256) % 256, int($fill_color) % 256);
		my $tc = dcExplorer::canvas::Color->new(int($text_color / 256 / 256) % 256, int($text_color / 256) % 256, int($text_color) % 256, 0);
		$self->{Canvas}->Text($self->{Canvas}->center($x, $y), 0, 0, $text, $tc, $text_size);
	}
}

1;

